var api = require('../../config/api.js');
var util = require('../../utils/util.js');
var user = require('../../utils/user.js');
var check = require('../../utils/check.js');

// pages/shop/shop.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgList: [],
    name: '',
    price: '',
    num: '',
    number: '',
    zz1: '',
    beizhu: '',
    god: '',
    good: {},
    id: '',
    shelf: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    if (options.id){
    this.setData({
      god: options.id
    })
    this.getshop()
    }
  },
  ChooseImage() {
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        console.log(res)
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
        let that = this
        wx.uploadFile({
          url: api.MultipleSave, //仅为示例，非真实的接口地址
          filePath: res.tempFilePaths[0],
          name: 'file',
          header: {
            "Content-Type": "multipart/form-data"
          },
          formData: {
            'user': 'test'
          },
          success(res) {
            console.log(res.data)
            var bb = JSON.stringify(res.data)
            console.log(bb.data)
            var sstr = bb.split("\"");
            var gg = sstr[sstr.length - 3]
            gg = gg.substring(0, gg.length - 1);
            console.log(gg)
            //do something
            that.setData({
              zz1: gg
            })
          }
        })

      }
    });
  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '操作提示',
      content: '确定要删除？',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },
  name(e) {
    console.log(e)
    this.setData({
      name: e.detail.value
    })
  },
  price(e) {
    this.setData({
      price: e.detail.value
    })
  },
  num(e) {
    this.setData({
      num: e.detail.value
    })

  },
  number(e) {
    this.setData({
      number: e.detail.value
    })
  },
  textareaBInput(e) {
    this.setData({
      beizhu: e.detail.value
    })
  },

  tijiao() {
    if (this.data.name == '') {
      wx.showToast({
        title: '商品名字不能为空',
        icon: 'none'
      })
      return false
    }
    if (this.data.price == '') {
      wx.showToast({
        title: '商品价格不能为空',
        icon: 'none'
      })
      return false
    }
    if (this.data.num == '') {
      wx.showToast({
        title: '商品货号不能为空',
        icon: 'none'
      })
      return false
    }
    if (this.data.zz1 == '') {
      wx.showToast({
        title: '商品缩略图不能为空',
        icon: 'none'
      })
      return false
    }
    if (this.data.number == '') {
      wx.showToast({
        title: '商品库存不能为空',
        icon: 'none'
      })
      return false
    }
    let that = this
    util.request(api.AddMerGoods, {
      goodsImg: that.data.zz1,
      goodsNum: that.data.num,
      inventoryNum: that.data.number,
      merchantCode: wx.getStorageSync("code"),
      name: that.data.name,
      remarks: that.data.beizhu,
      sellingPrice: that.data.price,
      shelf: that.data.shelf,
      id: that.data.id
    }, 'POST', {
      "x-merchant-request-code": wx.getStorageSync("code"),
      'Content-Type': 'application/json;charset=UTF-8',
      'token': wx.getStorageSync('token')
    }).then(function(res) {
      console.log(res)
      if (res.code == 200) {
        wx.showModal({
          title: '提交成功',
          content: '提交成功，亲等待我们的审核，通过后会立即联系你',
          showCancel: false,
          success: res => {
            wx.redirectTo({
              url: '/pages/manageshop/manageshop',
            })
          }
        });
        return false;
      }
      if(res.code!=200){
        wx.showToast({
          title: res.msg,
          icon:'none'
        })
      }
    });
  },



  xiugai() {
    if (this.data.name == '') {
      wx.showToast({
        title: '商品名字不能为空',
        icon: 'none'
      })
      return false
    }
    if (this.data.price == '') {
      wx.showToast({
        title: '商品价格不能为空',
        icon: 'none'
      })
      return false
    }
    if (this.data.num == '') {
      wx.showToast({
        title: '商品货号不能为空',
        icon: 'none'
      })
      return false
    }
    if (this.data.zz1 == '') {
      wx.showToast({
        title: '商品缩略图不能为空',
        icon: 'none'
      })
      return false
    }
    if (this.data.number == '') {
      wx.showToast({
        title: '商品库存不能为空',
        icon: 'none'
      })
      return false
    }
    let that = this
    util.request(api.UpdateGoodsInfo, {
      goodsImg: that.data.zz1,
      goodsNum: that.data.num,
      inventoryNum: that.data.number,
      merchantCode: wx.getStorageSync("code"),
      name: that.data.name,
      remarks: that.data.beizhu,
      sellingPrice: that.data.price,
      shelf: that.data.shelf,
      id: that.data.id,
      status: 0
    }, 'POST', {
      "x-merchant-request-code": wx.getStorageSync("code"),
      'Content-Type': 'application/json;charset=UTF-8',
      'token': wx.getStorageSync('token')
    }).then(function(res) {
      console.log(res)
      if (res.code == 200) {
        wx.showModal({
          title: '提交成功',
          content: '提交成功，亲等待我们的审核，通过后会立即联系你',
          showCancel: false,
          success: res => {
            wx.redirectTo({
              url: '/pages/manageshop/manageshop',
            })
          }
        });
        return false;
      }
      if (res.code != 200) {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    });
  },
  getshop() {
    let that = this
    util.request(api.QueryMerGoods + "/" + that.data.god, {

    }, 'get', {
      "x-merchant-request-code": wx.getStorageSync("code"),
      'Content-Type': 'application/json;charset=UTF-8',
      'token': wx.getStorageSync('token')
    }).then(function(res) {
      console.log(res)
      if (res.code == 200) {
        console.log(res)
        let bb = []
        bb.push(res.data.goodsInfo.goodsImg)
        that.setData({
          good: res.data.goodsInfo,
          imgList: bb,
          shelf: res.data.goodsInfo.shelf,
          id: res.data.goodsInfo.id,
          name: res.data.goodsInfo.name,
          price: res.data.goodsInfo.sellingPrice,
          num: res.data.goodsInfo.goodsNum,
          number: res.data.goodsInfo.inventoryNum,
          zz1: res.data.goodsInfo.goodsImg,
          beizhu: res.data.goodsInfo.remarks,
        });
      }
    });
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    user.checkLogin().then(res => {
    }).catch(() => {
      wx.clearStorageSync("token")
      wx.showModal({
        title: '温馨提示',
        content: '登录过期或未登录',
        showCancel: false,
        success: res => {
          wx.redirectTo({
            url: '/pages/login/login',
          })
        }
      })
    })
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})